package tom.adminlte.util;

import java.io.*;
import java.nio.charset.Charset;

/**
 * @author ZHUFEIFEI
 */
public class AdminLtePathTool {

    public static void main(String[] args) throws IOException {
        File inputDir = new File("D:\\Kingsoft\\idea-studyspace\\adminlte2.4.10" +
                "\\bower_components\\admin-lte");

        File outputDir = new File("D:\\data\\adminlte\\");

        if (!outputDir.exists()) {outputDir.mkdirs();}

        recursive(inputDir, outputDir);
    }

    private static void recursive(File inputDir, File outputDir) throws IOException {
        for (File f : inputDir.listFiles((f) -> f.isDirectory() || f.getName().endsWith("html"))) {
            if (f.isDirectory()) {
                recursive(f, outputDir);
            } else {
                transform(f, outputDir);
            }
        }
    }

    private static void transform(File f, File outputDir) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f), Charset.forName("utf-8")));
        File out = new File(
                outputDir.getAbsolutePath() +
                        f.getPath().substring(f.getParent().lastIndexOf(System.getProperty("file.separator")))
        );
        if (!out.exists()) {
            if (!out.getParentFile().exists()) {
                System.out.println("create dir : " + out.getAbsolutePath());
                out.getParentFile().mkdirs();
            }
            System.out.println("create file : " + out.getAbsolutePath());
            out.createNewFile();
        }
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(out), Charset.forName("utf-8")));
        String line;
        while ((line = br.readLine()) != null) {
            line = replaceRelativePath(line);
            bw.write(line);
            bw.newLine();
        }
        System.out.println("transform file : " + out.getAbsolutePath() + " complete!");
        br.close();
        bw.close();
    }

    private static String replaceRelativePath(String line) {
        line = line.replaceAll("\\.\\./\\.\\./bower_components", "../../../static");
        line = line.replaceAll("\\.\\./bower_components", "../../static");
        line = line.replaceAll("\"bower_components/", "\"static/");
        line = line.replaceAll("\\.\\./\\.\\./dist/", "../../../static/admin-lte/");
        line = line.replaceAll("\\.\\./dist/", "../../static/admin-lte/");
        line = line.replaceAll("\"dist/", "\"static/admin-lte/");
        line = line.replaceAll("\\.\\./\\.\\./plugins/", "../../../static/admin-lte/plugins/");
        line = line.replaceAll("\\.\\./plugins/", "../../static/admin-lte/plugins/");
        line = line.replaceAll("\"plugins/", "\"static/admin-lte/plugins/");
        line = line.replaceAll("/dist/", "/");
//        line = line.replaceAll("\\.\\./\\.\\./", "");
//        line = line.replaceAll("\\.\\./", "pages/");
        return line;
    }

    public static void main11(String[] args) throws IOException {
        String tmp = "<link rel=\"stylesheet\" href=\"../../bower_components/bootstrap/dist/css/bootstrap.min.css\">";
        System.out.println(replaceRelativePath(tmp));

        File outputDir = new File("D:/data/logs/");
        File file = new File("D:\\data\\logs\\cdn-fresh-receipt\\1.zip");

        System.out.println(file.getPath().substring(file.getParent().lastIndexOf(System.getProperty("file.separator"))));
        System.out.println(outputDir.getAbsolutePath() + "\\" + file.getName());

    }

}
